// 登录组件
<template>
  <div class="login_wrap">
    <!-- 登录盒子 -->
    <div class="login_box">
      <!-- 登录表单 -->
      <el-form
        align="right"
        :model="loginForm"
        status-icon
        :rules="loginFormRul"
        ref="loginFormRef"
        label-width="100px"
        class="login_form"
      >
        <el-form-item label="用户" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单数据绑定对象
      loginForm: {
        username: '',
        password: '',
      },
      // 表单验证规则对象
      loginFormRul: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 9,
            message: '用户名长度在 3 到 9 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '密码长度在 6 到 12 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    login() {
      //点击登录的时候先调用validate方法验证表单内容是否有误，预验证
      this.$refs.loginFormRef.validate(async (valid) => {
        //如果valid参数为false则不发送请求直接返回
        if (!valid) return
        //如果valid参数为true则验证通过，则发送请求进行登录
        const { data: res } = await this.$axios.post('login', this.loginForm)
        sessionStorage.setItem('username', res.data.username)
        console.log(res.data.username)
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败:' + res.meta.msg)
        }
        this.$message.success('登录成功')
        //保存当前的token
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    //重置表单
    resetForm() {
      // this=>当前组件对象，其中的属性$refs包含了设置的表单ref
      // console.log(this);
      this.$refs.loginFormRef.resetFields()
    },
  },
}
</script>

<style scoped>
.login_wrap {
  background: url(../assets/bgc.jpg);
  height: 100%;
}
.login_box {
  width: 450px;
  height: 250px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 1px 1px #ddd;
  opacity: 0.5;
}
.login_box:hover {
  opacity: 0.7;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
